package com.asset.web.purchase;

import com.asset.pojo.Purchase;
import com.asset.pojo.PurchaseDetail;
import com.asset.service.PurchaseService;
import com.asset.util.PageResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @author 11725
 */
@RestController
@RequestMapping("/purchase")
public class PurchaseController {

    @Resource
    private PurchaseService purchaseService;

    /**
     * 分页查询
     * @param page      页码
     * @param rows      行数
     * @param depaId    部门编号
     * @param status    审批状态
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<PageResult<Purchase>> selectByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "10") Integer rows,
            @RequestParam(value = "depaId", required = false) Long depaId,
            @RequestParam(value = "status", required = false) Integer status
    ) {
        return ResponseEntity.ok(purchaseService.selectByPage(page, rows, depaId, status));
    }

    /**
     * 根据 id 查询采购详情
     * @param id        采购单 id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<PurchaseDetail> selectDetail(@PathVariable("id") Long id) {
        return ResponseEntity.ok(purchaseService.selectById(id));
    }

    /**
     * 新增采购计划
     * @param applyFund 申请金额
     * @param detail    采购详单
     * @return
     */
    @PostMapping
    public ResponseEntity<PurchaseDetail> insert(
            @RequestParam("applyFund") BigDecimal applyFund,
            @RequestBody PurchaseDetail detail
    ) {
        purchaseService.insert(applyFund, detail);
        return ResponseEntity.ok(detail);
    }

    /**
     * 修改采购详情
     * @param detail    采购详情
     * @return
     */
    @PutMapping("/detail")
    public ResponseEntity<Void> updateDetail(@RequestBody PurchaseDetail detail) {
        purchaseService.updateDetail(detail);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 修改审批状态
     * @param purchase      Purchase 对象
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateStatus(@RequestBody Purchase purchase) {
        // TODO - 修改审批状态
        purchaseService.updateStatus(purchase);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable("id") Long id) {
        purchaseService.delete(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
